<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="true"
    :visible.sync="insideFlage"
    @close="addPermissionFormConfig.cancel()"
    class="vc-dialog-form"
    destroy-on-close
    title="新增权限"
    width="480px"
  >
    <supForm :formConfig="addPermissionFormConfig" ref="addPermissiondForm" />
    <div class="dialog-footer" slot="footer">
      <el-button size="small" @click="addPermissionFormConfig.cancel()">取消</el-button>
      <el-button size="small" :disabled="disabled" @click="  addPermissionFormConfig.ok()" type="primary">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import supForm from '_c/form'
import store from '@/store'
const SFGD = store.state.dic.listFormat['sfgd'].map(i => {
  i.title = i.name
  i.key = i.code
  return i
})
export default {
  name: 'addPermission',
  components: { supForm },
  props: {
    addPermission: {
      type: Boolean,
      default () {
        return false
      }
    },
    selectNode: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      insideFlage: false,
      disabled: false,
      // 新增权限
      addPermissionFormConfig: {
        items: [
          {
            title: '权限名称',
            key: 'name',
            maxlength: 20,
            required: true,
            message: '请输入名称'
          },
          {
            title: '权限编码',
            key: 'code',
            maxlength: 30,
            required: true,
            message: '请输入编码',
            rules: 'half'
          },
          {
            title: '描述',
            key: 'descr',
            type: 'textarea'
          }
        ],
        params: {
        },
        cancel: () => {
          this.$refs.addPermissiondForm.resetForm()
          this.insideFlage = false
          this.$emit('close', false)
        },
        ok: () => {
          let flage = this.$refs.addPermissiondForm.save()
          if (flage) {
            this.disabled = true
            this.$api
              .addPermission({
                data: this.addPermissionFormConfig.params
              })
              .then(res => {
                if (res.success) {
                  this.$message.success('新增成功')
                  this.$refs.addPermissiondForm.resetForm()
                  this.insideFlage = false
                  this.$emit('close', res)
                } else {
                  this.disabled = false
                }
              })
              .catch(err => {
                this.disabled = false
              })
          }
        }
      }
    }
  },
  watch: {
    addPermission (value) {
      if (value) {
        this.init(value)
      }
    }
  },
  methods: {
    init (value) {
      this.insideFlage = value
      this.disabled = false
      this.addPermissionFormConfig.params.mgroupId = this.selectNode.id
    }
  }
}
</script>
